package TakeAwaySystem;

import TakeAwaySystem.adminoperation.admin.AdminDeleteOperation;
import TakeAwaySystem.adminoperation.admin.AdminModifyOperation;
import TakeAwaySystem.adminoperation.admin.AdminQueryOperation;
import TakeAwaySystem.adminoperation.AdminIOperation;
import TakeAwaySystem.adminoperation.business.LockBusinessOperation;
import TakeAwaySystem.adminoperation.business.UnLockBusinessOperation;
import TakeAwaySystem.adminoperation.business.QueryBusinessOperation;

import java.io.*;
import java.util.*;

/**
 * @author Vv
 * @version 1.0
 * @project coding_learn
 * @description
 * @date 2023/9/25 22:13:26
 */
public class Admin extends User{
    public static final File file = new File("D:/TakeAwaySystem/Admin.txt");
    public static final File businessFile = new File("D:/TakeAwaySystem/Business.txt");
    public static final int PASSWORD = 2;
    public Admin(int id,String name,String password){
        super(id,name,password);
    }

    public Admin(){

    }


    @Override
    public String toString() {
        return "Admin{" +
                "password='" + password + '\'' +
                ", Id=" + Id +
                ", Name='" + Name + '\'' +
                '}';
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public int menu() {
        Scanner scanner = new Scanner(System.in);

        System.out.println("0.退出系统");
        System.out.println("1.显示管理员信息");// \n
        System.out.println("2.修改本管理员信息");// \n
        System.out.println("3.删除本管理员信息");// \n
        System.out.println("4.禁用商户权限");// \n
        System.out.println("5.解锁商户权限");
        System.out.println("6.查寻商户信息");
        return scanner.nextInt();
    }

    @Override
    public void Operation(int n) {
        AdminIOperation iOperation = null;
        switch(n){
            case 0:
                System.exit(0);
                break;
            case 1:
                iOperation = new AdminQueryOperation();
                break;
            case 2:
                iOperation = new AdminModifyOperation();
                break;
            case 3:
                iOperation = new AdminDeleteOperation();
                break;
            case 4:
                iOperation = new LockBusinessOperation();
                break;
            case 5:
                iOperation = new UnLockBusinessOperation();
                break;
            case 6:
                iOperation = new QueryBusinessOperation();
                break;
            default:
                System.out.println("输入错误 请重新输入");
        }
        if(n <= 3){
            iOperation.work(this,file);
        }else{
            iOperation.work(this,businessFile);
        }
    }
    @Override
    public User userLoginOrRegistered(File file) {
        Scanner  scanner = new Scanner(System.in);
        System.out.println("请先告诉我你选择登录或注册");
        System.out.println("1.登录");
        System.out.println("2.注册");
        int choise = 0;
        choise = scanner.nextInt();
        if(choise == 1){
            return Login(file);
        }else if(choise == 2) {
            return Registered(file);
        }
        return null;
    }
    private static User Login(File file){
        Scanner scanner= new Scanner(System.in);
        List<User> userList = fileToList(file);
        // List<User> userList = fileToList();
        Set<Integer> IdSet = new HashSet<>();
        // Set<String> NameSet = new HashSet<>();
        HashMap<Integer,String> hashMap = new HashMap<>();
        for(User user : userList){
            user = (Admin)user;
            //   NameSet.add(user.getName());
            IdSet.add(user.getId());
            hashMap.put(user.getId(),((Admin) user).getPassword());
        }
        boolean accountIsException = false;
        int Value = 0;
        do{
            System.out.println("首先输入 你的Id");
            Value = scanner.nextInt();
            accountIsException = IdSet.contains(Value);
            if(!accountIsException){
                System.out.println("这个账号不存在请重新输入");
            }
        }while(!accountIsException);
        //每一次只有三次的登录机会  否则先退出系统 拜拜了您
        int count = 3;
        do{
            System.out.println("接下来请输入密码");
            String password = scanner.next();
            if(password.equals(hashMap.get(Value))){
                System.out.println("登录成功");
                for(User user : userList){
                    if(user.getId() == Value){
                        return new Admin(user.getId(),user.getName(),password);
                    }
                }
            }else{
                System.out.println("密码错误请重新输入");
                count--;
            }
        }while (count > 0);
        if(count <= 0){
            System.exit(0);
        }
        return null;
    }
    private static User Registered(File file) {
        System.out.println("现在我们开始创建账号");
        Scanner scanner = new Scanner(System.in);
        List<User> userList = fileToList(file);
        //  List<User> userList = fileToList();
        Set<Integer> IdSet = new HashSet<>();
        Set<String> NameSet = new HashSet<>();
        for (User user : userList) {
            IdSet.add(user.getId());
            NameSet.add(user.getName());
        }
        String Name = null;
        //注册名字  不能有重复的  不能有空格！！！！！
        {
            System.out.println("请输入你要创建管理员的名字");
            do {
                Name = scanner.next();
                if (NameSet.contains(Name)) {
                    System.out.println("这个名字已经被注册了 请你换一个吧");
                } else if (Name.contains(" ")) {
                    System.out.println("名字不能存在空格");
                }
            } while (NameSet.contains(Name) && !Name.contains(" "));
        }
        //随机出来一个Id
        int newId = 0;
        {
            do {
                newId = (int) (Math.random() * (900000)) + 100000;
            } while (IdSet.contains(newId) && (newId >= 100000 && newId <= 999999));
        }
        String password = null;
        //密码要求出现大小写字母加数字 其他的不允许出现
        {
            //密码要求出现大小写字母加数字 其他的不允许出现
            System.out.println("密码必须要出现大小写字母和数字 其他符号不允许出现 且大于等于八位数");
            System.out.println("请输入第一次密码 ： ");
            boolean isRight = true;
            do {

                //直到给我收到第一次密码合格 再进行重复测试
                do {
                    password = scanner.next();
                    char[] str = password.toCharArray();
                    isRight = str.length >= 8 ? true : false;
                    System.out.print(isRight ? "密码长度合格" : "密码长度不合格");
                    for (int i = 0; str.length >= 8 && i < str.length; i++) {
                        if (!(str[i] >= '0' && str[i] <= '9') && !(str[i] >= 'a' && str[i] <= 'z') && !(str[i] >= 'A' && str[i] <= 'Z')) {
                            System.out.println("出现了除大小写字母和数字 之外的字符");
                            isRight = false;
                        }
                    }
                    System.out.println(isRight ? "密码合格" : "密码不合格");

                } while (!isRight);
                System.out.println("请重复输入密码 要求和上次输入的密码相同");
                String pass2 = scanner.next();
                isRight = pass2.equals(password);
                System.out.println(isRight ? "密码成功 开始注册" : " 密码错误 重新输入");
            } while (!isRight);
        }
        User user = new Admin(newId,Name,password);
        userList.add(user);
        ListToFile(userList,file);
        System.out.println("你的ID是" + newId +"请牢记");
        System.out.println("你的ID是" + newId +"请牢记");
        System.out.println("你的ID是" + newId +"请牢记");
        return user;
    }
    private static List<User> fileToList(File file)  {
        List<User> list = new ArrayList<>();
        //File file = new File("d:/Business.txt");
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(file);
            br = new BufferedReader(fr);
            while(true){
                String str = br.readLine();
                if(str != null && !str.isEmpty()){
                    String[] arr = str.split(" ");
                    Admin users = new Admin();
                    users.setId(Integer.valueOf(arr[0]));
                    users.setPassword(arr[1]);
                    users.setName(arr[2]);
                    list.add(users);
                }else{
                    break;
                }
            }
        }catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                fr.close();
                br.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }

        return list;
    }
    private static void ListToFile(List<User> list,File file) {
        // File file = new File("d:/Business.txt");
        FileWriter fileWriter = null;
        BufferedWriter bw = null;
        try {
            fileWriter = new FileWriter(file);
            bw = new BufferedWriter(fileWriter);
            int N = list.size();
            for(int i = 0;i<N;i++){
                Admin user = (Admin) list.get(i);
                StringBuilder sbr = new StringBuilder();
                sbr.append(user.getId() + " ");
                sbr.append(user.getPassword() + " ");
                sbr.append(user.getName());
                bw.write(sbr.toString());
                bw.newLine();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            try {
                bw.close();
                fileWriter.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }

    }
}
